/**
 * 
 */


(function(){
    var App = TM.App = TM.App || {}
})();



TM.loadScript("app", "game-element", function(){
    
    if (!TM.App.GameElement) { return false; }
    
    /**
     * @lends   TM.App.Scene.prototype
     */
    var Scene = TM.App.Scene = TM.createClass({
        
        superClass: TM.App.GameElement,
        
        /**
         * シーンとして使用するゲームエレメントクラス
         * @constructs
         * @class       シーンクラス
         * @extends     TM.App.GameElement
         * @see <a href="./../../test/app/scene-test.html">Test Program</a>.
         */
        init: function() {
            TM.App.GameElement.prototype.init.apply(this);
            
            this.background = "black";
        },
        
    });
    
    
    var StartingScene = TM.App.StartingScene = TM.createClass({
        
        superClass: TM.App.Scene,
        
        init: function() {
            this.superInit(TM.App.Scene);
            
            var word = TM.App.Text();
            word.position.set(160, 160);
            word.align      = "center";
            word.baseline   = "center";
            word.height     = 320;
            word.width      = 320;
            word.text       = "START";
            this.addChild(word);
        },
        
        update: function() {
            if (app.keyboard.getKeyDown("space") || app.pointing.getPointingStart()) {
                app.scene = this.nextScene;
            }
        },
        
    });
    
    var EndingScene = TM.App.EndingScene = TM.createClass({
        
        superClass: TM.App.Scene,
        
        init: function() {
            this.superInit(TM.App.Scene);
            
            var word = TM.App.Text();
            word.position.set(160, 160);
            word.align      = "center";
            word.baseline   = "center";
            word.height     = 320;
            word.width      = 320;
            word.text       = "END";
            this.addChild(word);
        },
        
        update: function(app) {
            if (app.keyboard.getKeyDown("space") || app.pointing.getPointingStart()) {
                this.dispatchEvent("9leapresult");
            }
        },
        
        on9leapresult: function(e) {
            if (location.hostname == 'r.jsgames.jp') {
                var submit = function() {
                    var url = "http://9leap.net/games/{id}/result?score={score}&result={result}".format({
                        id: location.pathname.match(/^\/games\/(\d+)/)[1],
                        score: encodeURIComponent(this.app.score),
                        result: encodeURIComponent(this.app.result)
                    });
                    location.replace(url);
                }
                submit();
            }
        },
        
    });

});














